﻿using Kimd.Common;
using Kimd.Communication;
using Kimd.Logic;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;

namespace WorkStation.Logic
{
    /// <summary>
    /// 测试治具1/2
    /// </summary>
    public class SimTesterLogic : FlowBase
    {
        private int _testerIndex;
        private bool[] _resultReceivedFlag;
        public Tray TesterTray { get; set; }
        public TesterStatus CurrentTesterStatus { get; set; }
        public SimTesterLogic(string flowName, FlowManager flowmanager, int testerIndex) 
            : base(flowName, flowmanager)
        {
            this._testerIndex = testerIndex;
            this.TesterTray = new Tray(1, 4);
            this._resultReceivedFlag = new bool[2] { false, false };

            isFromRetest = false;
        }

        public override void Calibrate()
        {
            base.Calibrate();
        }

        public override void Home()
        {
            CurrentTesterStatus = TesterStatus.Idle;
        }

        public override void Init(out string errInfo)
        {
            base.Init(out errInfo);
        }

        public override void InSingleRunMode()
        {
            base.InSingleRunMode();
        }

        DateTime ctStartCountTime;
        DateTime ctEndCountTime;
        bool isFromRetest = false;
        public override void Method()
        {
            if (!CheckFlowCondition()) return;

            switch (CurrentTesterStatus)
            {
                case TesterStatus.Idle:

                    WaitForSignal("治具上料模组", $"测试治具{_testerIndex + 1}放料完成");
                    if (!CheckFlowCondition()) return;

                    LogHelper.Instance.Log(this.Description, $"{this.Description} 状态刷新为Testing...", MessageLevel.Log);
                    CurrentTesterStatus = TesterStatus.Testing;

                    ctStartCountTime = DateTime.Now;
                retest:
                    if (!GlobalVariable.IsDryRun && !SystemConfig.Instance.UsingStatus.IsTesterSimulate)
                    {
                        LogHelper.Instance.Log(this.Description, $"{this.Description} 开始发送 StartTest", MessageLevel.Log);
                        //if (isFromRetest)
                        //{
                        //    isFromRetest = false;
                        //    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                        //    {
                        //        OpenVac(_testerIndex, 0, true);
                        //    }
                        //    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                        //    {
                        //        OpenVac(_testerIndex, 1, true);
                        //    }
                        //}
                        //SendPackage sp1 = null;
                        //SendPackage sp2 = null;
                        //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && !TesterTray.Helper.GetCell(0).IsPreNG)
                        //{
                        //    SendPackage sendPackage = new SendPackage();
                        //    sendPackage.Message = $"StartTest,{_testerIndex},{0},{TesterTray.Helper.GetCell(0).SN},{0},{GlobalVariable.MesConfig}";
                        //    StartTest(_testerIndex, 0, TesterTray.Helper.GetCell(0).SN, 0, out sp1);
                        //}
                        //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && !TesterTray.Helper.GetCell(1).IsPreNG)
                        //{
                        //    SendPackage sendPackage = new SendPackage();
                        //    sendPackage.Message = $"StartTest,{_testerIndex},{1},{TesterTray.Helper.GetCell(1).SN},{0},{GlobalVariable.MesConfig}";
                        //    StartTest(_testerIndex, 1, TesterTray.Helper.GetCell(1).SN, 0, out sp2);
                        //}
                        //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && !TesterTray.Helper.GetCell(0).IsPreNG)
                        //{
                        //    if (!WaitStartTest(_testerIndex, 0, sp1))
                        //    {
                        //        isFromRetest = true;
                        //        goto retest;
                        //    }
                        //    _resultReceivedFlag[0] = false;
                        //}
                        //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && !TesterTray.Helper.GetCell(1).IsPreNG)
                        //{
                        //    if (!WaitStartTest(_testerIndex, 1, sp2))
                        //    {
                        //        isFromRetest = true;
                        //        goto retest;
                        //    }
                        //    _resultReceivedFlag[1] = false;
                        //}
                    }

                    break;

                case TesterStatus.Testing:

                    LogHelper.Instance.Log(this.Description, $"{this.Description} 等待测试完成", MessageLevel.Log);
                    DateTime startTime = DateTime.Now;
                    TimeSpan ts = DateTime.Now - startTime;
                    while (!AllEnabledTesterFinished() && ts.TotalMilliseconds < 120000 && !IsBreak())
                    {
                        ts = DateTime.Now - startTime;
                        Thread.Sleep(10);
                    }
                    if (!CheckFlowCondition()) return;
                    if (!AllEnabledTesterFinished())
                    {
                        if (ts.TotalMilliseconds >= 120000)
                        {
                            ShowWarnAndPause($"等待测试测试模组{_testerIndex + 1}测试完成超时!测试完成状态:{string.Join(",", _resultReceivedFlag)}", "系统");
                            if (MessageBox.Show("是否重发测试指令?", "测试", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                            {
                                isFromRetest = true;
                                goto retest;
                            }
                        }
                    }
                    if (GlobalVariable.IsDryRun || SystemConfig.Instance.UsingStatus.IsTesterSimulate)
                    {
                        if (GlobalVariable.IsGrrMode || GlobalVariable.IsCosmeticMode)
                        {
                            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"当前GRR模式，强制OK", this.Description, 0, ""));
                        }
                        else
                        {
                            if (SystemConfig.Instance.UsingStatus.IsEnforceOK)
                            {
                                if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                    TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeOK);
                                else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                    TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeOK);
                                else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeOK);

                                if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                    TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeOK);
                                else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                    TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeOK);
                                else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);

                                if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(2).Result == TestResult.FirstTimeNG)
                                    TesterTray.Helper.SetCellResult(2, TestResult.SecondTimeOK);
                                else if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(2).Result == TestResult.SecondTimeNG)
                                    TesterTray.Helper.SetCellResult(2, TestResult.ThirdTimeOK);
                                else if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct)
                                    TesterTray.Helper.SetCellResult(2, TestResult.FirstTimeOK);

                                if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(3).Result == TestResult.FirstTimeNG)
                                    TesterTray.Helper.SetCellResult(3, TestResult.SecondTimeOK);
                                else if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(3).Result == TestResult.SecondTimeNG)
                                    TesterTray.Helper.SetCellResult(3, TestResult.ThirdTimeOK);
                                else if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct)
                                    TesterTray.Helper.SetCellResult(3, TestResult.FirstTimeOK);
                            }
                            else
                            {
                                Random rd = new Random();
                                int val = rd.Next(1, 10);
                                if (val <= 1)
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                    {
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeNG);
                                    }
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeNG);
                                    }

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                    {
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeNG);
                                    }
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeNG);
                                    }

                                    if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(2).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(2, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(2).Result == TestResult.SecondTimeNG)
                                    {
                                        TesterTray.Helper.SetCellResult(2, TestResult.ThirdTimeNG);
                                    }
                                    else if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(2, TestResult.FirstTimeNG);
                                    }

                                    if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(3).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(3, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(3).Result == TestResult.SecondTimeNG)
                                    {
                                        TesterTray.Helper.SetCellResult(3, TestResult.ThirdTimeNG);
                                    }
                                    else if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(3, TestResult.FirstTimeNG);
                                    }
                                }
                                else if (val >= 2 && val <= 3)
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeOK);

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);

                                    if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(2).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(2, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(2).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(2, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(2, TestResult.FirstTimeOK);

                                    if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(3).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(3, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(3).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(3, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(3, TestResult.FirstTimeOK);
                                }
                                else if (val > 3 && val <=7)
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeNG);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeNG);

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);

                                    if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(2).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(2, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(2).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(2, TestResult.ThirdTimeNG);
                                    else if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(2, TestResult.FirstTimeNG);

                                    if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(3).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(3, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(3).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(3, TestResult.ThirdTimeNG);
                                    else if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(3, TestResult.FirstTimeNG);
                                }
                                else if (val > 7)
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeOK);

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeNG);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeNG);

                                    if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(2).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(2, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(2).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(2, TestResult.ThirdTimeNG);
                                    else if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(2, TestResult.FirstTimeNG);

                                    if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(3).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(3, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(3).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(3, TestResult.ThirdTimeNG);
                                    else if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(3, TestResult.FirstTimeNG);
                                }
                                else
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeNG);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeNG);

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);

                                    if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(2).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(2, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(2).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(2, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(2).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(2, TestResult.FirstTimeOK);

                                    if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(3).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(3, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(3).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(3, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(3).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(3, TestResult.FirstTimeOK);
                                }
                            }
                        }
                    }
                    this._resultReceivedFlag = new bool[2] { false, false };
                    CurrentTesterStatus = TesterStatus.TestFinish;
                    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"{this.Description}测试完成", this.Description, 0, "系统"));
                    ctEndCountTime = DateTime.Now;
                    LogHelper.Instance.Log(this.Description, $"测试时间:{(ctEndCountTime - ctStartCountTime).TotalMilliseconds}", MessageLevel.Log);

                    break;

                case TesterStatus.TestFinish:

                    WaitForSignal("下料逻辑", $"取测试治具{_testerIndex + 1}完成");
                    if (!CheckFlowCondition()) return;
                    CurrentTesterStatus = TesterStatus.Idle;

                    break;

                case TesterStatus.Disable:

                    //处于禁用状态下，每隔一段时间刷新tester启用状态来看一下

                    break;
            }
        }

        public override void OnStop()
        {
            base.OnStop();
        }

        public override bool PreStartCheck()
        {
            return base.PreStartCheck();
        }

        public override void SpotCheck()
        {
            base.SpotCheck();
        }
        int count = 0;
        public bool AllEnabledTesterFinished(int bankIndex = 2)
        {
            if (GlobalVariable.IsDryRun || SystemConfig.Instance.UsingStatus.IsTesterSimulate)
            {
                Thread.Sleep(10);
                count++;
                if (count >= SystemConfig.Instance.TestParam.TestTimeCount)
                {
                    count = 0;
                    return true;
                }
                else
                    return false;
            }
            bool allEnabledTesterFinish = true;

            //if (bankIndex == 2)
            //{
            //    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct &&
            //        !TesterTray.Helper.GetCell(0).IsPreNG)
            //    {
            //        if (!_resultReceivedFlag[0])
            //            allEnabledTesterFinish = false;
            //    }
            //    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct &&
            //        !TesterTray.Helper.GetCell(1).IsPreNG)
            //    {
            //        if (!_resultReceivedFlag[1])
            //            allEnabledTesterFinish = false;
            //    }
            //    if (TesterTray.Helper.GetCell(0).IsPreNG)
            //    {
            //        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeNG);
            //    }
            //    if (TesterTray.Helper.GetCell(1).IsPreNG)
            //    {
            //        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeNG);
            //    }
            //}
            //else if (bankIndex == 0)
            //{
            //    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct &&
            //        !TesterTray.Helper.GetCell(0).IsPreNG)
            //    {
            //        if (!_resultReceivedFlag[0])
            //            allEnabledTesterFinish = false;
            //    }
            //    if (TesterTray.Helper.GetCell(0).IsPreNG)
            //    {
            //        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeNG);
            //    }
            //}
            //else if (bankIndex == 1)
            //{
            //    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct &&
            //        !TesterTray.Helper.GetCell(1).IsPreNG)
            //    {
            //        if (!_resultReceivedFlag[1])
            //            allEnabledTesterFinish = false;
            //    }
            //    if (TesterTray.Helper.GetCell(1).IsPreNG)
            //    {
            //        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeNG);
            //    }
            //}

            return allEnabledTesterFinish;
        }
    }
    public enum TesterStatus
    {
        [Description("空闲")]
        Idle,
        [Description("测试中")]
        Testing,
        [Description("测试完成")]
        TestFinish,
        [Description("禁用")]
        Disable
    }
}
